package org.jeecg.modules.system.rule;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.handler.IFillRuleHandler;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.erp.base.entity.SysSequence;
import org.jeecg.modules.erp.base.mapper.SysSequenceMapper;

import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
public class SequenceNoRule implements IFillRuleHandler {
    @Override
    public Object execute(JSONObject params, JSONObject formData) {
        log.info("系统自定义编码规则[sequence_no_rule]，params：{} ，formData： {}", params, formData);
        String   name="";
        //订单前缀默认为CN 如果规则参数不为空，则取自定义前缀
        if (params != null) {
            Object objName = params.get("name");
            if (objName != null){
                   name = objName.toString();
            }else {
                return null;
            }
        }
        SysSequenceMapper mapper = (SysSequenceMapper) SpringContextUtils.getBean("sysSequenceMapper");
        Integer maxValByName = mapper.getMaxValByName(name);
        if (maxValByName != null) {
            SysSequence sysSequence = mapper.selectOne(new LambdaQueryWrapper<SysSequence>().eq(SysSequence::getName, name).last(" limit 1"));
            if (ObjectUtil.isNull(sysSequence)){
              return null;
            }
            if (ObjectUtil.isNotNull(sysSequence)) {
                String returnStr="";
                Date updateTime = sysSequence.getUpdateTime();

                SimpleDateFormat format = new SimpleDateFormat("yyMM");
                String updateTimeStr = format.format(updateTime);
                String nowStr = format.format(new Date());
                switch (name){
                    case "物料编号":
                        sysSequence.setVal(maxValByName + 1);
                        mapper.updateById(sysSequence);
                        returnStr="M"+String.format("%010d", sysSequence.getVal());
                        break;
                    case "配方编码":

                        if (!nowStr.equalsIgnoreCase(updateTimeStr)){
                            maxValByName=0;
                        }
                        sysSequence.setVal(maxValByName + 1);
                        mapper.updateById(sysSequence);
                        returnStr="CPPF"+"-"+nowStr+"-"+String.format("%04d", sysSequence.getVal());
                        break;
                    case "配方公式编码":
                        if (!nowStr.equalsIgnoreCase(updateTimeStr)){
                            maxValByName=0;
                        }
                        sysSequence.setVal(maxValByName + 1);
                        mapper.updateById(sysSequence);
                        returnStr="PFGS"+"-"+nowStr+"-"+String.format("%04d", sysSequence.getVal());
                        break;
                    case "合同评审说明编码":
                        if (!nowStr.equalsIgnoreCase(updateTimeStr)){
                            maxValByName=0;
                        }
                        sysSequence.setVal(maxValByName + 1);
                        mapper.updateById(sysSequence);
                        returnStr="HTPS"+"-"+nowStr+"-"+String.format("%04d", sysSequence.getVal());
                        break;
                    case "出库单编码":
                        if (!nowStr.equalsIgnoreCase(updateTimeStr)){
                            maxValByName=0;
                        }
                        sysSequence.setVal(maxValByName + 1);
                        mapper.updateById(sysSequence);
                        returnStr="CK-CC"+nowStr+"-"+String.format("%04d", sysSequence.getVal());
                        break;
                    case "合同评审表编码":
                        if (!nowStr.equalsIgnoreCase(updateTimeStr)){
                            maxValByName=0;
                        }
                        sysSequence.setVal(maxValByName + 1);
                        mapper.updateById(sysSequence);
                        returnStr="PSB-"+nowStr+"-"+String.format("%04d", sysSequence.getVal());
                        break;
                    case "入库单编码":
                        if (!nowStr.equalsIgnoreCase(updateTimeStr)){
                            maxValByName=0;
                        }
                        sysSequence.setVal(maxValByName + 1);
                        mapper.updateById(sysSequence);
                        returnStr="RK-CC"+nowStr+"-"+String.format("%04d", sysSequence.getVal());
                        break;
                    case "采购请购单编码":
                        if (!nowStr.equalsIgnoreCase(updateTimeStr)){
                            maxValByName=0;
                        }
                        sysSequence.setVal(maxValByName + 1);
                        mapper.updateById(sysSequence);
                        returnStr="CLQG-"+nowStr+"-"+String.format("%04d", sysSequence.getVal());
                        break;
                    case "采购计划单编码":
                        if (!nowStr.equalsIgnoreCase(updateTimeStr)){
                            maxValByName=0;
                        }
                        sysSequence.setVal(maxValByName + 1);
                        mapper.updateById(sysSequence);
                        returnStr="YCJH-"+nowStr+"-"+String.format("%04d", sysSequence.getVal());
                        break;
                    case "采购订单编码":
                        if (!nowStr.equalsIgnoreCase(updateTimeStr)){
                            maxValByName=0;
                        }
                        sysSequence.setVal(maxValByName + 1);
                        mapper.updateById(sysSequence);
                        returnStr="YCJH-"+nowStr+"-"+String.format("%04d", sysSequence.getVal());
                        break;
                    case "原料价格执行表编码":
                        if (!nowStr.equalsIgnoreCase(updateTimeStr)){
                            maxValByName=0;
                        }
                        sysSequence.setVal(maxValByName + 1);
                        mapper.updateById(sysSequence);
                        returnStr="YJZX-"+nowStr+"-"+String.format("%04d", sysSequence.getVal());
                        break;
                    case "采购合同评审单编码":
                        if (!nowStr.equalsIgnoreCase(updateTimeStr)){
                            maxValByName=0;
                        }
                        sysSequence.setVal(maxValByName + 1);
                        mapper.updateById(sysSequence);
                        returnStr="CGSP-"+nowStr+"-"+String.format("%04d", sysSequence.getVal());
                        break;
                    case "入库通知单编码":
                        if (!nowStr.equalsIgnoreCase(updateTimeStr)){
                            maxValByName=0;
                        }
                        sysSequence.setVal(maxValByName + 1);
                        mapper.updateById(sysSequence);
                        returnStr="RKTZD-"+nowStr+"-"+String.format("%04d", sysSequence.getVal());
                        break;
                    case "原材料包材检验单编码":
                        if (!nowStr.equalsIgnoreCase(updateTimeStr)){
                            maxValByName=0;
                        }
                        sysSequence.setVal(maxValByName + 1);
                        mapper.updateById(sysSequence);
                        returnStr="YCLBCJYD-"+nowStr+"-"+String.format("%04d", sysSequence.getVal());
                        break;
                    case "扣货单据编号":

                        if (!nowStr.equalsIgnoreCase(updateTimeStr)){
                            maxValByName=0;
                        }
                        sysSequence.setVal(maxValByName + 1);
                        mapper.updateById(sysSequence);
                        returnStr="KH-"+nowStr+"-"+String.format("%04d", sysSequence.getVal());
                        break;
                    case "盘点单编号":

                        if (!nowStr.equalsIgnoreCase(updateTimeStr)){
                            maxValByName=0;
                        }
                        sysSequence.setVal(maxValByName + 1);
                        mapper.updateById(sysSequence);
                        returnStr="PDD-"+nowStr+"-"+String.format("%04d", sysSequence.getVal());
                        break;
                    case "检验标准编号":

                        if (!nowStr.equalsIgnoreCase(updateTimeStr)){
                            maxValByName=0;
                        }
                        sysSequence.setVal(maxValByName + 1);
                        mapper.updateById(sysSequence);
                        returnStr="JYBZ-"+nowStr+"-"+String.format("%04d", sysSequence.getVal());
                        break;
                    case "不合格处理单编号":

                        if (!nowStr.equalsIgnoreCase(updateTimeStr)){
                            maxValByName=0;
                        }
                        sysSequence.setVal(maxValByName + 1);
                        mapper.updateById(sysSequence);
                        returnStr="BHGCLD-"+nowStr+"-"+String.format("%04d", sysSequence.getVal());
                        break;
                }
                return returnStr;
            }
        }
        return null;
    }
}
